package com.systemCapacity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemCapacity.common.dto.BattlePlatformDeviceDto;
import com.systemCapacity.common.dto.BattlePlatformDto;
import com.systemCapacity.common.dto.BlueBattlePlatformDto;
import com.systemCapacity.common.dto.BlueEquipmentDto;
import com.systemCapacity.entity.*;
import com.systemCapacity.mapper.BlueBattlePlatformMapper;
import com.systemCapacity.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class BlueBattlePlatformServiceImpl extends ServiceImpl<BlueBattlePlatformMapper, BlueBattlePlatform> implements BlueBattlePlatformService{

    @Autowired
    BattlePlatformDeviceService battlePlatformDeviceService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    AttributeService attributeService;

    @Autowired
    BattleCapacityService battleCapacityService;

    @Autowired
    DeviceService deviceService;


    @Override
    public void savePlatForm(List<BlueBattlePlatformDto> platformList) {
        for (BlueBattlePlatformDto battlePlatformDto : platformList) {
            BlueBattlePlatform battlePlatform = new BlueBattlePlatform();
            BeanUtil.copyProperties(battlePlatformDto,battlePlatform);
            battlePlatform.setPlatformId(UUID.randomUUID().toString());
            save(battlePlatform);
            List<BattlePlatformDeviceDto> platformDeviceList = battlePlatformDto.getPlatformDeviceList();
            platformDeviceList.forEach(x -> x.setPlatformId(battlePlatform.getPlatformId()));
            battlePlatformDeviceService.saveBattlePlatformDevice(platformDeviceList);
        }
    }

    @Override
    public List<BlueBattlePlatformDto> queryPlatformListByTaskId(String taskId) {
        QueryWrapper<BlueBattlePlatform> queryPlatform = new QueryWrapper<>();
        queryPlatform.eq("taskId",taskId);
        queryPlatform.orderByAsc("platformNum");

        List<BlueBattlePlatform> battlePlatformList = list(queryPlatform);
        List<BlueBattlePlatformDto> battlePlatformDtoList = battlePlatformList.stream().map(x -> {
            BlueBattlePlatformDto battlePlatformDto = new BlueBattlePlatformDto();
            BeanUtil.copyProperties(x, battlePlatformDto);
            QueryWrapper<Equipment> queryEquipment = new QueryWrapper<>();
            queryEquipment.eq("equipmentId",x.getEquipmentId());
            Equipment equip = equipmentService.getOne(queryEquipment);
            battlePlatformDto.setEquipmentName(equip.getEquipmentName());
            battlePlatformDto.setEquipmentWeight(equip.getWeight());
            return battlePlatformDto;
        }).collect(Collectors.toList());

        //查询平台设备
        if (!battlePlatformDtoList.isEmpty()) {
            for (BlueBattlePlatformDto battlePlatformDto : battlePlatformDtoList) {
                QueryWrapper<BattlePlatformDevice> deviceQueryWrapper = new QueryWrapper<>();
                deviceQueryWrapper.eq("platformId",battlePlatformDto.getPlatformId());
                List<BattlePlatformDevice> battlePlatformDeviceList = battlePlatformDeviceService.list(deviceQueryWrapper);
                if(battlePlatformDeviceList.isEmpty()){
                    battlePlatformDto.setPlatformDeviceList(new ArrayList<>());
                }else{
                    List<BattlePlatformDeviceDto> battlePlatformDeviceDtoList = battlePlatformDeviceList.stream().map(x -> {
                        BattlePlatformDeviceDto battlePlatformDeviceDto = new BattlePlatformDeviceDto();
                        BeanUtil.copyProperties(x, battlePlatformDeviceDto);
                        //查询能力
                        QueryWrapper<BattleCapacity> capacityQueryWrapper = new QueryWrapper<>();
                        capacityQueryWrapper.eq("capacityId",x.getCapacityId());
                        BattleCapacity battleCapacity = battleCapacityService.getOne(capacityQueryWrapper);
                        if(battleCapacity != null){
                            battlePlatformDeviceDto.setCapacityName(battleCapacity.getCapacityName());
                        }
                        //查询设备
                        QueryWrapper<Device> queryDevice = new QueryWrapper<>();
                        queryDevice.eq("deviceId",x.getDeviceId());
                        Device device = deviceService.getOne(queryDevice);
                        battlePlatformDeviceDto.setDeviceWeight(device.getWeight());
                        return battlePlatformDeviceDto;
                    }).collect(Collectors.toList());
                    battlePlatformDto.setPlatformDeviceList(battlePlatformDeviceDtoList);
                }
                //查询设备
                List<BlueEquipmentDto> equipmentBlueAttributeList = attributeService.getEquipmentBlueAttributes(battlePlatformDto.getEquipmentId());
                List<String> equipmentNameList = equipmentBlueAttributeList.stream().map(x -> x.getEquipmentName()).collect(Collectors.toList());
                battlePlatformDto.setEquipmentNameList(equipmentNameList);
                battlePlatformDto.setEquipmentList(equipmentBlueAttributeList);
            }
        }
        return battlePlatformDtoList;
    }

    @Override
    public void deletePlatformByTaskId(String taskId) {
        QueryWrapper<BlueBattlePlatform> battlePlatformQueryWrapper = new QueryWrapper<>();
        battlePlatformQueryWrapper.eq("taskId",taskId);
        //查询平台id
        List<BlueBattlePlatform> battlePlatformList = list(battlePlatformQueryWrapper);
        List<String> platformIdList = battlePlatformList.stream().map(BlueBattlePlatform::getPlatformId).collect(Collectors.toList());
        //删除平台装备
        if (!platformIdList.isEmpty()) {
            battlePlatformDeviceService.deletePlatformDeviceByPlatformIdList(platformIdList);
            //删除平台数据
            remove(battlePlatformQueryWrapper);
        }
    }

}
